package tech.aistar.day02;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 本类用来演示: 环绕通知
 *
 * @author: success
 * @date: 2021/5/7 1:34 下午
 */
@Component
@Aspect
@Order(2)
public class CheckParamAspect {

    @Around("execution (* tech.aistar.day02.*.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        //模拟的是一个前置通知
        //获取目标方法的名称
        System.out.println("before-method-name:"+joinPoint.getSignature().getName());
        //获取目标方法的实参列表
        System.out.println("before-method-params:"+Arrays.toString(joinPoint.getArgs()));
        Object result = null;
        //调用目标方法
        try {
           result =  joinPoint.proceed();

            System.out.println("method-result:"+result);

            System.out.println("后置返回通知");
        } catch (Throwable throwable) {
            //异常通知
            System.out.println("异常通知:"+throwable.getMessage());
            throwable.printStackTrace();
        }finally {
            //后置通知
            System.out.println("后置通知");
        }
        return result;
    }
}
